Vue.component('yl-button', {
    template: `
    <span>
        <el-button :type="type" @click="Export" :loading="loading">
            <slot></slot>
        </el-button>
        <el-table id="ExportData" :data="data" style="display: none; width: 100%;margin-bottom: 20px;">
            <el-table-column label="序号"type="index"></el-table-column>
            <el-table-column prop="Supplier_Article_SerialNumber" label="供应商品内码"></el-table-column>
            <el-table-column prop="Article_SerialNumber" label="商品内码"></el-table-column>
            <el-table-column prop="Article_Specifications_SerialNumber" label="规格内码"></el-table-column>
            <el-table-column prop="Supplier_SerialNumber" label="供应商内码"></el-table-column>
            <el-table-column prop="Article_Name" label="商品名称"></el-table-column>
            <el-table-column prop="Specifications_Name" label="规格名称"></el-table-column>
            <el-table-column prop="Picture_URL" label="图片">
                <template v-slot="{row}">
                    {{row.Picture_URL? BASE_IMG +row.Picture_URL:'否'}}
                </template>
            </el-table-column>
            <el-table-column prop="Brand_Name" label="品牌名称"></el-table-column>
            <el-table-column prop="Brand_SerialNumber" label="品牌内码"></el-table-column>
            <el-table-column prop="Category_Name" label="类别"></el-table-column>
            <el-table-column prop="Category_SerialNumber" label="类别内码"></el-table-column>
            <el-table-column prop="Series_Name" label="系列"></el-table-column>
            <el-table-column prop="Series_SerialNumber" label="系列内码"></el-table-column>
            <el-table-column prop="Total_stock" label="今日上架库存"></el-table-column>
            <el-table-column prop="Stock_Remain" label="当日剩余库存"></el-table-column>
            <el-table-column prop="Stock_Sell" label="当日已售"></el-table-column>
            <el-table-column prop="Shelf" label="是否上架">
                <template v-slot="{row}">
                    {{row.Shelf=='0'?'是':'否'}}
                </template>
            </el-table-column>
            <el-table-column prop="Shelf_Date" label="上/下架操作时间"></el-table-column>
            <el-table-column label="导出时间">
                <template v-slot="{row}">
                    {{getYMDHMSDate()}}
                </template>
            </el-table-column>
        </el-table>
    </span>`,
    props: ['loading', 'data', 'type'],
    methods: {
        Export() {
            this.$emit('click')
        }
    }
})
new Vue({
    el: '#app',
    data() {
        return {
            form: {
                SQLSerialNumber: "P_supplier_selection_article",
                PaginationNumber: 1,//页数
                RowAmount: 10,//每页数据条数
                Article_Name:'',
                Supplier_SerialNumber:'',
                Brand_SerialNumber:'',
                selection:false,//已选
                ExtensionCondition:'',
                Condition:''
            },
            total:0,
            tableData: [],	//表格数据
            brand:[],
            ExportData:[],//导出
            loading: false,
            defaultProps: {
                children: 'Children',
                label: 'Brand_Name'
            },
            supplier:[],
            multipleSelection:[],
        }
    },
    mounted() {
        if (getCookie('role')) {
            let roleObj = JSON.parse(getCookie('role'));
            this.form.Supplier_SerialNumber = roleObj.other;
        }
        this.getData();
        this.getList(this.form);//前端分页
    },
    methods: {
        onSerach() {			//查询方法
            this.form.PaginationNumber = 1;
            this.getList(this.form)
        },
        getData(){
            const that = this ;
            $.ajax({
                headers:requestHeaders(),
                type: 'POST',
                url: `${BASE_URL2}/generalBusiness/generalQueryByTableName`,
                data:{
                    Data:'business_brand',
                    Condition:"Row_Status='0'"
                },
                success: function (Result) {
                    that.brand = Result
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        selectable(row) {
            return row.Supplier_Article_SerialNumber == ''
        },
        handleSelectionChange(val){//批量选品
            this.multipleSelection = val;
        },
        downloadTemplate(command) {
            if (command == "Article") {
                window.location.href = './download/导入商品模板表.新.xlsx';
            }
        },
        importTemplate(command) {
            if (command == "Article") {
                window.location.href = './import.html';
            }
        },
        handleBatchAdd() {//批量添加
            const that = this;
            if (this.multipleSelection.length > 0) {
                this.$confirm('是否将勾选的商品加入到已选商品中?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'info'
                }).then(() => {
                    //1.遍历已选查询对应数据
                    let sql = '';
                    this.multipleSelection.forEach(item => {
                        if (sql) {
                            sql = sql + "or Supplier_SerialNumber='" + that.form.Supplier_SerialNumber + "' and Article_Specifications_SerialNumber='" + item.Article_Specifications_SerialNumber + "' \r\n"
                        } else {
                            sql = "Supplier_SerialNumber='" + that.form.Supplier_SerialNumber + "' and Article_Specifications_SerialNumber='" + item.Article_Specifications_SerialNumber + "' \r\n";
                        }
                    });
                    $.ajax({
                        headers:requestHeaders(),
                        type: 'POST',
                        url: `${BASE_URL2}/generalBusiness/generalQueryByTableNameWithOriginal`,
                        data: { 
                            Data: "business_supplier_article" ,
                            Condition: "(" + sql + ")",
                        },
                        success: function (business_supplier_article) {
                            let Add = {business_supplier_article:[]};
                            let Modify = {business_supplier_article:[]}
                            for (let i = 0; i < that.multipleSelection.length; i++) {
                                let obj = that.multipleSelection[i];
                                let is = true;
                                for (let j = 0; j < business_supplier_article.length; j++) {
                                    if (obj.Article_Specifications_SerialNumber == business_supplier_article[j].Article_Specifications_SerialNumber) {//存在
                                        business_supplier_article[j].Row_Status = '0';
                                        Modify.business_supplier_article.push(business_supplier_article[j]);
                                        is = false;
                                        break;
                                    }
                                }
                                if (is) { 
                                    Add.business_supplier_article.push({
                                        Supplier_Article_SerialNumber:(that.form.Supplier_SerialNumber+'-'+obj.Article_Specifications_SerialNumber),
                                        Supplier_SerialNumber:that.form.Supplier_SerialNumber,
                                        Article_SerialNumber:obj.Article_SerialNumber,
                                        Article_Specifications_SerialNumber:obj.Article_Specifications_SerialNumber,
                                        Shelf:'0',
                                        Shelf_Date:getYMDHMSDate(),
                                    })
                                }
                            }
                            $.ajax({
                                headers:requestHeaders(),
                                type: 'POST',
                                url: `${BASE_URL2}/generalBusiness/generalSave`,
                                data:{
                                    Data:JSON.stringify({
                                        Modify,Add
                                    })
                                },
                                success: function (Result) {
                                    if (Result.Status == 'success') {
                                        that.$message({
                                            type: 'success',
                                            message: '选品成功!'
                                        });
                                    }else{
                                        that.$message.error("系统繁忙，请重试")
                                    }
                                    that.getList(that.form);
                                },
                                complete: function (XMLHttpRequest) {
                                    requestPretreatment(XMLHttpRequest);
                                },
                                dataType: "json"
                            });
                        },
                        complete: function (XMLHttpRequest) {
                            requestPretreatment(XMLHttpRequest);
                        },
                        dataType: "json"
                    });
                }).catch(() => { });
            } else {
                this.$message({
                    type: 'warning',
                    message: '至少要选择1个商品!'
                });
            }
        },
        Add(){
            window.location.href = '../Add.html'
        },
        handleAdd(row){//添加
            const that = this;
            let Supplier_Article_SerialNumber = row.Supplier_Article_SerialNumber;
            $.ajax({
                headers:requestHeaders(),
                type: 'POST',
                url: `${BASE_URL2}/generalBusiness/generalQueryByTableNameWithOriginal`,
                data: { 
                    Data: "business_supplier_article" ,
                    Condition: "Article_Specifications_SerialNumber='" + row.Article_Specifications_SerialNumber 
                    + "' and Supplier_SerialNumber='" + that.form.Supplier_SerialNumber + "'"
                },
                success: function (business_supplier_article) {
                    let data = {};
                    if (Supplier_Article_SerialNumber) {//取消
                        business_supplier_article[0].Row_Status = '1' 
                        data = {
                            Data: JSON.stringify({ Modify: { business_supplier_article } })
                        }
                    }else {//添加
                        if (business_supplier_article.length == 1) {//存在
                            business_supplier_article[0].Row_Status = '0' 
                            data = {
                                Data: JSON.stringify({ Modify: { business_supplier_article } })
                            }
                        } else if (business_supplier_article.length == 0) {//不存在
                            business_supplier_article.push({
                                Supplier_Article_SerialNumber:(that.form.Supplier_SerialNumber+'-'+row.Article_Specifications_SerialNumber),
                                Supplier_SerialNumber:that.form.Supplier_SerialNumber,
                                Article_SerialNumber:row.Article_SerialNumber,
                                Article_Specifications_SerialNumber:row.Article_Specifications_SerialNumber,
                                Shelf:'0',
                                Shelf_Date:getYMDHMSDate(),
                            })
                            data = {
                                Data: JSON.stringify({ Add: { business_supplier_article } })
                            }
                        }
                    }
                    $.ajax({
                        headers:requestHeaders(),
                        type: 'POST',
                        url: `${BASE_URL2}/generalBusiness/generalSave`,
                        data,
                        success: function (Result) {
                            if (Result.Status == 'success') {
                                that.$message({
                                    type: 'success',
                                    message: Supplier_Article_SerialNumber?'取消成功' : '选品成功!'
                                });
                            }else{
                                that.$message.error("系统繁忙，请重试")
                            }
                            that.getList(that.form);
                        },
                        complete: function (XMLHttpRequest) {
                            requestPretreatment(XMLHttpRequest);
                        },
                        dataType: "json"
                    });
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        Add() {
            window.location.href = './add.html'
        },
        examine(){//审核明细
            window.location.href = './log.html'
        },
        handleSizeChange(val) {
            this.form.PaginationNumber = 1;
            this.form.RowAmount = val;
            this.getList(this.form)
        },
        handleCurrentChange(val) {
            this.form.PaginationNumber = val;
            this.getList(this.form)
        },
        getList(form) {//换页方法
            const that = this
            let data = deepClone(form);
            data.Condition = window.btoa(unescape(encodeURIComponent(data.Supplier_SerialNumber+",%"+data.Article_Name+"%")));
            let sql = "";
            if (data.Brand_SerialNumber) {
                sql = sql + "and business_article_info.Brand_SerialNumber = '"+data.Brand_SerialNumber+"'\r\n";
            }
            if (data.selection) {
                sql = sql + "and business_supplier_article.Supplier_Article_SerialNumber <> ''\r\n"
            }
            data.ExtensionCondition  = window.btoa(unescape(encodeURIComponent(sql)));
            $.ajax({
                headers:requestHeaders(),
                type: 'POST',
                url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
                data,
                success: function (Result) {
                    that.tableData = Result.Rows;
                    that.total = Result.TotalRows;
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        Export(form){
            const that = this;
            this.loading = true;
            let data = deepClone(form);
            data.Condition = window.btoa(unescape(encodeURIComponent("%"+data.Article_Name+"%")));
            let sql = "";
            if (data.Supplier_SerialNumber) {
                sql = sql + "and business_supplier_article.Supplier_SerialNumber = '"+data.Supplier_SerialNumber+"'\r\n";
            }
            if (data.Brand_SerialNumber) {
                sql = sql + "and business_article_info.Brand_SerialNumber = '"+data.Brand_SerialNumber+"'\r\n";
            }
            data.ExtensionCondition  = window.btoa(unescape(encodeURIComponent(sql)));
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryBySQLSerialNumber`,
                type: 'POST',
                data,
                success: function (Result) {
                    if (Result.length > 0) {
                        that.ExportData = Result;
                        that.$nextTick(() => {
                            let wb = XLSX.utils.table_to_book(document.querySelector("#ExportData"));
                            var wbout = XLSX.write(wb, {
                                bookType: "xlsx",
                                bookSST: true,
                                type: "array"
                            });
                            try {
                                saveAs(
                                    new Blob([wbout], {
                                        type: "application/octet-stream"
                                    }),
                                    "供应商品信息表"+ (+new Date()) + ".xlsx"
                                );
                            } catch (e) {
                                if (typeof console !== "undefined") console.log(e, wbout);
                            }
                            that.$message({
                                message: '已成功导出',
                                type: 'success'
                            })
                            return wbout;
                        })
                    }
                },
                complete: function (XMLHttpRequest) {
                    that.loading = false;
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        }
    },
})